package com.example.hdz.floatwindowdemo.floatwindow;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.hdz.floatwindowdemo.R;
import com.example.hdz.floatwindowdemo.Util;

/**
 *
 * @author hdz
 * @date 2018/11/17
 */

public class FloatWindowBigView extends LinearLayout implements View.OnClickListener, View.OnTouchListener {
    /**
     * 记录大悬浮窗的宽度
     */
    public static int viewWidth;

    /**
     * 记录大悬浮窗的高度
     */
    public static int viewHeight;

    private Rect mViewRect;
    private ImageButton mImgBtnPlayPause;
    private boolean isPlaying = true;

    public FloatWindowBigView(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.float_window_big, this);
        View view = findViewById(R.id.big_window_layout);
        viewWidth = view.getLayoutParams().width;
        viewHeight = view.getLayoutParams().height;

        ImageButton imgBtnHome = (ImageButton)findViewById(R.id.home);
        imgBtnHome.setOnClickListener(this);
        imgBtnHome.setOnTouchListener(this);

        mImgBtnPlayPause = (ImageButton)findViewById(R.id.pause);
        mImgBtnPlayPause.setOnClickListener(this);
        mImgBtnPlayPause.setOnTouchListener(this);

        ImageButton imgBtnNext = (ImageButton)findViewById(R.id.next);
        imgBtnNext.setOnClickListener(this);
        imgBtnNext.setOnTouchListener(this);

        ImageButton imgBtnPrevious = (ImageButton)findViewById(R.id.previous);
        imgBtnPrevious.setOnClickListener(this);
        imgBtnPrevious.setOnTouchListener(this);

        ImageButton imgBtnDisconnect = (ImageButton)findViewById(R.id.disconnect);
        if (imgBtnDisconnect != null) {
            imgBtnDisconnect.setOnClickListener(this);
            imgBtnDisconnect.setOnTouchListener(this);
        }
        setOnTouchListener(this);

        mViewRect = new Rect();
        this.getGlobalVisibleRect(mViewRect);
    }

    public FloatWindowBigView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public FloatWindowBigView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public FloatWindowBigView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home:
                Toast.makeText(getContext(), "主页", Toast.LENGTH_SHORT).show();
                break;
            case R.id.previous:
//                Toast.makeText(getContext(), "上一曲", Toast.LENGTH_SHORT).show();
                Util.musicPrevious(getContext());
                break;
            case R.id.next:
//                Toast.makeText(getContext(), "下一曲", Toast.LENGTH_SHORT).show();
                Util.musicNext(getContext());
                break;
            case R.id.pause:
                Util.musicPlayOrPause(getContext());
                if (isPlaying) {
                    isPlaying = false;
                    mImgBtnPlayPause.setImageResource(R.drawable.play_64px);
//                    Toast.makeText(getContext(), "暂停", Toast.LENGTH_SHORT).show();
                } else {
                    isPlaying = true;
                    mImgBtnPlayPause.setImageResource(R.drawable.pause_64px);
//                    Toast.makeText(getContext(), "播放", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.disconnect:
                Toast.makeText(getContext(), "断开", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v.getId()==R.id.home || v.getId()==R.id.next || v.getId()==R.id.previous || v.getId()==R.id.pause) {
            return onImageButtonTouch(v, event);
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_CANCEL:
                break;
            case MotionEvent.ACTION_UP:
                int x = (int)event.getX();
                int y = (int)event.getY();
                if (!mViewRect.contains(x, y)) {
                    // 点击大悬浮之外的区域，移除大悬浮窗，创建小悬浮窗
                    MyWindowManager.getInstance().showSmallWindow(getContext());
                }
                break;
            default:
                break;
        }
        return true;
    }

    private boolean onImageButtonTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            v.setAlpha(0.5f);
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            v.setAlpha(1);
        }
        return false;
    }
}
